// Town 5: Arena Center
// t5Arena.txt
// This is the town script for the Arena Center in the HLPM.
// 
// Most of the real work here is done in the INIT_STATE, the START_STATE, and
// the EXIT_STATE. The Arena also uses the creature script Arena Fan, almost
// identical to nullmonst.txt, for the audience members, who (as part of the
// START_STATE) cheer.
//
// State 10 is for stairs.

begintownscript;

variables;
// Standard vars
short i,j,k,won;
string dlgstr;

body;

beginstate INIT_STATE;
	
	set_crime_tolerance(1);

	// Get rid of those ugly braziers. They're only there for light.
	set_terrain(29,39,0);
	set_terrain(44,34,0);
	set_terrain(40,18,0);
	
	// Make it impossible to combat-bug down the stairs.
	change_blocked(44,18,1);
	change_blocked(44,19,1);
	change_blocked(44,20,1);
	change_blocked(45,18,1);
	change_blocked(45,19,1);
	change_blocked(45,20,1);
	
// Create the combats. The level is flag (4,0). The flag (4,2) defines whether
// it is a ranged/missile combat (4,2 == TRUE) or not.

// The idea for the melee combats is that four of the monsters are melee, and
// the last one is a spellcaster. At level 45 and beyond, though, they're all
// spellcasters.

// The idea for the missile combats is much the same. Three of the monsters
// primarily use missile weapons, and one is a magic-user. At level 45 and
// beyond, as before, they're all magic-users.

	// If there is no combat, end everything.
	if (get_flag(4,1) == 0)
		end();
	
	// Place the opponents. First, melee combats.
if (get_flag(4,2) == 0) {
	if (get_flag(4,1) == 1) {
		place_monster(39,32,24,0); // Thug L2
		place_monster(40,32,24,0);
		place_monster(42,32,24,0);
		place_monster(43,32,24,0);
		place_monster(41,33,26,0); // Acolyte L4
		end();
		}
		
	if (get_flag(4,1) == 5) {
		place_monster(39,32,28,0); // Nephil L5
		place_monster(40,32,28,0);
		place_monster(42,32,28,0);
		place_monster(43,32,28,0);
		place_monster(41,33,30,0); // Nephil Shaman L7
		end();
		}
		
	if (get_flag(4,1) == 10) {
		place_monster(39,32,34,0); // Slith L10
		place_monster(40,32,34,0);
		place_monster(42,32,34,0);
		place_monster(43,32,34,0);
		place_monster(41,33,37,0); // Slith mage L12
		end();
		}
		
	if (get_flag(4,1) == 15) {
		place_monster(39,32,19,0); // Assassin L14
		place_monster(40,32,19,0);
		place_monster(42,32,19,0);
		place_monster(43,32,19,0);
		place_monster(41,33,89,0); // Evil high priest L18
		end();
		}
		
	if (get_flag(4,1) == 20) {
		place_monster(39,32,39,0); // Cave giant L20
		place_monster(40,32,39,0);
		place_monster(42,32,39,0);
		place_monster(43,32,39,0);
		place_monster(41,33,40,0); // Giant Shaman L20
		end();
		}
		
	if (get_flag(4,1) == 25) {
		place_monster(39,32,20,0); // Elite Soldier L25
		place_monster(40,32,20,0);
		place_monster(42,32,20,0);
		place_monster(43,32,20,0);
		place_monster(41,33,24,0); // High priest L25
		end();
		}
		
	if (get_flag(4,1) == 30) {
		place_monster(39,32,96,0); // Royal guard L30
		place_monster(40,32,96,0);
		place_monster(42,32,96,0);
		place_monster(43,32,96,0);
		place_monster(41,33,97,0); // Royal mage L30
		end();
		}
		
	if (get_flag(4,1) == 35) {
		place_monster(39,32,223,0); // Alien Beast L34
		place_monster(40,32,223,0);
		place_monster(42,32,223,0);
		place_monster(43,32,223,0);
		place_monster(41,33,154,0); // Vahnatai Keeper L36
		end();
		}
		
	if (get_flag(4,1) == 40) {
		place_monster(39,32,175,0); // Empire Dervish L40
		place_monster(40,32,175,0);
		place_monster(42,32,175,0);
		place_monster(43,32,175,0);
		place_monster(41,33,105,0); // Triad mage L40
		end();
		}
		
	if (get_flag(4,1) == 45) {
		place_monster(39,32,156,0); // Vahnatai Lord L45
		place_monster(40,32,156,0);
		place_monster(42,32,156,0);
		place_monster(43,32,156,0);
		place_monster(41,33,156,0);
		end();
		}
		
	if (get_flag(4,1) >= 50)
		activate_hidden_group(1);
		
	// For combats greater than level 50, just set the dragons to a higher level
	// using set_level.
	if (get_flag(4,1) > 50) {
		set_level(21,get_flag(4,1));
		set_level(22,get_flag(4,1));
		set_level(23,get_flag(4,1));
		set_level(24,get_flag(4,1));
		set_level(25,get_flag(4,1));
		}
	}
	
	// If it wasn't a melee combat, place a missile combat.
	else {
	if (get_flag(4,1) == 1) {
		place_monster(33,32,9,0); // Brigand Archer L2
		place_monster(33,33,9,0);
		place_monster(33,34,9,0);
		place_monster(32,33,26,0); // Acolyte L4
		end();
		}
		
	if (get_flag(4,1) == 5) {
		place_monster(33,32,24,0); // Nephil Archer L5
		place_monster(33,33,24,0);
		place_monster(33,34,24,0);
		place_monster(32,33,30,0); // Nephil Shaman L7
		end();
		}
		
	if (get_flag(4,1) == 10) {
		place_monster(33,32,124,0); // Rogue Archer L12
		place_monster(33,33,124,0);
		place_monster(33,34,124,0);
		place_monster(32,33,13,0); // Evil priest L12
		end();
		}
		
	if (get_flag(4,1) == 15) {
		place_monster(33,32,212,0); // Hill Giant L15
		place_monster(33,33,212,0);
		place_monster(33,34,212,0);
		place_monster(32,33,214,0); // Hill Giant Shaman L18
		end();
		}
		
	if (get_flag(4,1) == 20) {
		place_monster(33,32,39,0); // Cave giant L20
		place_monster(33,33,39,0);
		place_monster(33,34,39,0);
		place_monster(32,33,40,0); // Giant shaman L18
		end();
		}
		
	if (get_flag(4,1) == 25) {
		place_monster(33,32,228,0); // Spine Beast L25
		place_monster(33,33,228,0);
		place_monster(33,34,228,0);
		place_monster(32,33,60,0); // Demon L25
		end();
		}
		
	if (get_flag(4,1) == 30) {
		place_monster(33,32,225,0); // Dark Wyrm L30
		place_monster(33,33,225,0);
		place_monster(33,34,225,0);
		place_monster(32,33,222,0); // Mind Crystal L30
		end();
		}
		
	if (get_flag(4,1) == 35) {
		place_monster(33,32,135,0); // Augmented Giant L35
		place_monster(33,33,135,0);
		place_monster(33,34,135,0);
		place_monster(32,33,227,0); // Drake Lord L30
		end();
		}
		
	if (get_flag(4,1) == 40) {
		place_monster(33,32,155,0); // Vahnatai Blade L40
		place_monster(33,33,155,0);
		place_monster(33,34,155,0);
		place_monster(32,33,105,0); // Triad mage L40
		end();
		}
		
	if (get_flag(4,1) >= 45)
		activate_hidden_group(2);
		
	// If the combat level is higher than 45, level-edit the lords to the right
	// level.
	if (get_flag(4,1) > 45) {
		set_level(26,get_flag(4,1));
		set_level(27,get_flag(4,1));
		set_level(28,get_flag(4,1));
		set_level(29,get_flag(4,1));
		}
	}

break;

beginstate EXIT_STATE;
// If no fight, end the state.
if (get_flag(4,1) == 0)
	end();
	
	// Check if NPCs are alive inside Arena rectangle (32,26) to (45,35). If
	// not, then the party has won a combat.
	k = 1; // holds whether party has won
	i = 32;
	while (i <= 45) {
		j = 26;
		while (j <= 35) {
			if (char_on_spot(i,j) > 0)
				k = 0;
			j = j + 1;
			}
		i = i + 1;
		}
		
	// If the party has won, tell them and reset the flags.
	if (k) {
		message_dialog("You win! While there is no tangible reward for a successful battle in the Arena, you can of course feel pride in your accomplishments.","");
		set_flag(4,2,0);
		set_flag(4,1,0);
		}
break;

beginstate START_STATE;

// Punish crime with instant death.
if (get_crime_level() >= 1) {
	kill_char(1000,2,0);
	message_dialog("Crimes in the HLPM are punishable by instant death, as you discover the hard way.","");
	}
	
// If fight is on, make sporadic cheering in the crowd.
if (get_flag(4,1) == 0)
	end();
	
	j = 6;
	while (j <= 70)
		{i = get_ran(1,1,50);
		if (i == 1)
			text_bubble_on_char(j,"Yay!");
		if (i == 2)
			text_bubble_on_char(j,"*cheers*");
		if (i == 3)
			text_bubble_on_char(j,"w00t!");
		j = j + 1;
		}

break;

beginstate 10; // Stairs down to Arena Entrance
	reset_dialog();
	add_dialog_str(0,"These stairs lead to the Arena entrance. What would you like to do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Descend the stairs.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(4,18,21);
break;